<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

include( "../global/adodb-time.inc.php" );

$tag = getPostValue( 'tag' );
$tag = str_replace( ' ', '', $tag );
$tag = strtolower( $tag );
$score = round( getPostValue( 'score' ) );
$history_id = getPostValue( 'history_id' );
$type = getPostValue( 'type' );

if( $tag != '' && is_numeric( $score ) && $score > 0 && is_numeric( $history_id ) && $history_id > 0 ) {
	$query = "SELECT rating_points " .
    		"FROM users " .
    		"WHERE user_id = " . $_SESSION[ 'user_id' ];
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	
	if( $row[ 'rating_points' ] >= $score ) {
		$query = "SELECT ruler_id, anonymous " .
	    		"FROM history " .
	    		"WHERE history_id = " . $history_id;
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		
		if( isset( $row[ 'ruler_id' ] ) && $row[ 'ruler_id' ] != -1 && $row[ 'ruler_id' ] != $_SESSION[ 'ruler_id' ] ) {
			$anonymous = $row[ 'anonymous' ]; 
			$writer_id = $row[ 'ruler_id' ]; 
			$positive = $negative = 0;
			if( $type == 'negative' ) {
				$negative ++;
			}
			else {
				$positive ++;
			}

			$query = "SELECT tag_id " .
		    		"FROM history_tags " .
		    		"WHERE tag = '$tag'";
		    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( !isset( $row[ 'tag_id' ] ) ) {
				$query = "INSERT INTO history_tags ( positive, negative, tag )" .
						" VALUES( $positive, $negative, '$tag')";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$query = "SELECT * " .
			    		"FROM history_tags " .
			    		"WHERE tag = '$tag'";
			    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				$tag_id = $row[ 'tag_id' ];
			}
			else {
				$tag_id = $row[ 'tag_id' ];
				$query = "UPDATE history_tags " .
						"SET positive = positive + $positive, negative = negative + $negative " .
						"WHERE tag_id = $tag_id";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$query = "SELECT count(*) " .
			    		"FROM history_tags " .
			    		"WHERE tag_id = $tag_id AND positive >= negative";
			    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				if( $row[ 'count(*)' ] > 0 ) {
					$positive = 1;
					$negative = 0;
				}
				else {
					$positive = 0;
					$negative = 1;
				}
			}
			
			$abs_score = $score;
			if( $negative == 1 ) {
				$score *= -1;
			}
			
			$query = "SELECT score " .
		    		"FROM history_rating " .
		    		"WHERE tag_id = $tag_id AND history_id = $history_id";
		    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( isset( $row[ 'score' ] ) ) {
				$query = "UPDATE history_rating " .
						"SET score = score + $abs_score " .
						"WHERE tag_id = $tag_id AND history_id = $history_id";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			else {
				$query = "INSERT INTO history_rating " .
						"( history_id, score, tag_id ) " .
						"VALUES( $history_id, $abs_score, $tag_id )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			
			$query = "UPDATE history_tags " .
					"SET score = score + $abs_score " .
					"WHERE tag_id = $tag_id";
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			
			$query = "UPDATE users " .
					"SET rating_points = rating_points - $abs_score " .
					"WHERE user_id = " . $_SESSION[ 'user_id' ];
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			
			$for_author = $score;
			if( $anonymous == 1 ) {
				$for_author *= 0.5;
			}
			else {
				$for_author *= 0.75;
			}
			
			$query = "UPDATE users " .
					"JOIN ruler ON ruler.user_id = users.user_id " .
					"SET rating_points = rating_points + $for_author " .
					"WHERE ruler_id = $writer_id";
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			
			$status = translate( "Score added" );
		}
		else {
			$status = translate( "You are not allowed to rate this story." );
		}
	}
	else {
		$status = translate( "You don't have enough rating points to give this score" ) . " ($score).";
	}
}
else {
	$status = translate( "Score entered must be more then zero and tag field cannot be empty." );
}

$_SESSION[ 'history_rating_status' ] = $status;
if( isset( $history_id ) ) {
	header( "Location: history.php?id=" . $history_id );
}
else {
	header( "Location: history.php" );
}
?>